<?php
namespace Admin\Model;
use Think\Model;
class ArticleClassModel extends Model {
	protected $tableName = 'article_class'; 
	public $grade; // 多层结构用一维数组显示
	protected $threearr; // 多层结构用三维数组显示
	protected $_validate = array(
		array('name','require','名称不能为空！'), //默认情况下用正则进行验证
	);

	/**
	 * 三维分级数组
	 */
	protected function three() {
		$list = $this->select();
		$arr = array();
		foreach($list as $k=>$v) {
			$arr[$v['pid']][$v['id']] = $v;
		}
		$this->threearr = $arr;
	}

	/**
	 * 用id做下标的分类列表
	 */
	public function kselect($where='') {
		$list = $this->where($where)->select();
		$arr = array();
		if(is_array($arr)) {
			foreach($list as $key=>$value) {
				$arr[$value['id']] = $value;
			}
		}
		return $arr;
	}

	/**
	 * 分级
	 * $pid 从那栏目开始往下分
	 * $num 级数 
	 */
	public function mselect($pid=0,$num=0) {
		if(empty($this->threearr)) {
			$this->three();
		}

		if(is_array($this->threearr[$pid])) {
			foreach($this->threearr[$pid] as $k=>$v) {
				$numn = $num;
				$v['grade'] = $numn;
				$this->grade[] = $v;
				if(is_array($this->threearr[$v['id']])) {
					$numn++;
					$this->mselect($v['id'],$numn);
				}
			}
		}
	}
	
	/**
	 * 添加、修改
	 */
	public function msave() {
		$data['name'] = I('post.name','','strip_tags');
		$data['pid'] = I('post.pid','','intval');
		$data['type'] = I('post.type','','intval'); // 1新闻 2单页 3试题 4留言
		$data['nav'] = I('post.nav',0,'intval');
		$data['link'] = I('post.link','','strip_tags');
		$where['id'] = I('post.id','','intval');
		if(empty($where['id'])) { //判断是添加还是修改
			$msg = $this->add($data);
		} else {
			$msg = $this->where($where)->save($data);
		}
		return $msg;
	}

	/**
	 * 单页列表
	 */
	public function page() {
		$where['type'] = C('PAGE');
		$list = $this->where($where)->select();
		return $list;
	}

	/**
	 * 修改单页
	 */
	public function editpage() {
		$where['id'] = I('post.id','','intval');
		$data['content'] = I('post.content');
		$msg = $this->where($where)->save($data);
		return $msg;
	}
}
